package com.cloudsea.yunhaistores.controller;


import com.cloudsea.yunhaistores.service.MerchantOrderService;
import com.cloudsea.yunhaistores.vo.OrderDetailVO;
import com.cloudsea.yunhaiutils.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商家-订单管理", description = "商家用于查询和管理订单的接口")
@RestController
@RequestMapping("/merchant/orders") // 使用 /merchant 前缀，区分用户端接口
public class MerchantOrderController {

    @Autowired
    private MerchantOrderService merchantOrderService;

    @Operation(summary = "查询新订单列表", description = "商家端轮询此接口，获取所有已支付、待处理(制作中)的新订单")
    @GetMapping("/new")
    public ApiResponse<List<OrderDetailVO>> listNewOrders(
            @Parameter(description = "门店ID", required = true)
            @RequestParam Long storeId
    ) {
        // 在这里，我们应该加入我们之前讨论过的、完整的安全校验，确保操作员有权访问此门店
        // (此处暂时简化)

        List<OrderDetailVO> newOrders = merchantOrderService.listNewOrders(storeId);
        return ApiResponse.success(newOrders);
    }

    @Operation(summary = "标记订单为“备餐完成”", description = "后厨完成备餐后，调用此接口将订单状态更新为“待取餐/待配送”")
    @PostMapping("/{orderId}/ready")
    public ApiResponse<Void> markOrderAsReady(
            @Parameter(description = "要操作的订单ID", required = true)
            @PathVariable Long orderId
    ) {
        merchantOrderService.markAsReadyForPickup(orderId);
        return ApiResponse.success("操作成功，订单已更新为待取餐", null);
    }

    /**
     * 手动完成订单
     * @param orderId 要完成的订单ID
     * @return ApiResponse
     */
    @Operation(summary = "手动完成订单", description = "服务员在顾客取餐后，调用此接口将订单状态更新为“已完成”")
    @PostMapping("/{orderId}/complete")
    public ApiResponse<Void> completeOrder(
            @Parameter(description = "要操作的订单ID", required = true)
            @PathVariable Long orderId
    ) {
        merchantOrderService.completeOrder(orderId);
        return ApiResponse.success("操作成功，订单已完成", null);
    }
}
